﻿using nERP.Model.Fin;
using nERP.Model.Po;
using nERP.Model.Rpt;
using nERP.Model.So;
using nERP.Model;
using nERP.ViewModel.Rpt.DashboardVMs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;
using WalkingTec.Mvvm.Core.Extensions;
using WalkingTec.Mvvm.Mvc.Admin.ViewModels.DataPrivilegeVMs;

namespace nERP.ViewModel.HomeVMs
{
    /// <summary>
    /// 仪表盘数据
    /// </summary>
    public class DashboardData
    { /// <summary>
      /// 检验数量全部统计
      /// </summary>
        public int TestAllCount { get; init; }

        /// <summary>
        /// 检验数量年统计
        /// </summary>
        public int TestYearCount { get; init; }

        /// <summary>
        /// 检验数量月统计
        /// </summary>
        public int TestMonthCount { get; init; }

        /// <summary>
        /// 检验数量日统计
        /// </summary>
        public int TestDayCount { get; init; }

        /// <summary>
        /// 日检验签发数量
        /// </summary>
        public int TestApprovedDayCount { get; init; }

        /// <summary>
        /// 日检验签发占比
        /// </summary>
        public double TestApprovedDayScale { get; set; }

        /// <summary>
        /// 月检验签发数量
        /// </summary>
        public int TestApprovedMonthCount { get; init; }

        /// <summary>
        /// 月检验签发占比
        /// </summary>
        public double TestApprovedMonthScale { get; set; }

        /// <summary>
        /// 年检验签发数量
        /// </summary>
        public int TestApprovedYearCount { get; init; }

        /// <summary>
        /// 年检验签发占比
        /// </summary>
        public double TestApprovedYearScale { get; set; }

        /// <summary>
        /// 全部检验签发数量
        /// </summary>
        public int TestApprovedAllCount { get; init; }

        /// <summary>
        /// 全部检验签发占比
        /// </summary>
        public double TestApprovedAllScale { get; set; }

        /// <summary>
        /// 当年月分组统计
        /// </summary>
        public List<TestDayGroupData> TestDayGroupList { get; init; } = [];

        /// <summary>
        /// KKS分类分组统计
        /// </summary>
        public List<TestKKSGroupData> TestKKSGroupList { get; init; } = [];



    }

    public class DashboardDatas : BaseVM
    {
        public Dashboard GetData()
        {
            var query = DC.Set<Dashboard>()

                .Select(x => new Dashboard
                {
                    ID = x.ID,

                    Monthly = x.Monthly,
                    PoAmount = DC.Set<Buy>().Sum(z => z.TotalPrice),
                    PoAmounted = DC.Set<Buy>().Where(z => z.Status == StatusTypeEnum.Reviewed).Sum(z => z.TotalPrice),
                    SoAmount = DC.Set<Sale>().Sum(z => z.TotalPrice),
                    SoAmounted = DC.Set<Sale>().Where(z => z.Status == StatusTypeEnum.Reviewed).Sum(z => z.TotalPrice),
                    PayAmount = DC.Set<Payment>().Sum(z => z.TotalPrice),
                    PayAmounted = DC.Set<Payment>().Where(z => z.Status == StatusTypeEnum.Reviewed).Sum(z => z.TotalPrice),
                    ReceivableAmount = DC.Set<Receivable>().Sum(z => z.TotalPrice),
                    ReceivableAmounted = DC.Set<Receivable>().Where(z => z.Status == StatusTypeEnum.Reviewed).Sum(z => z.TotalPrice),
                }).SingleOrDefault();
            return query;
        }
    }

    /// <summary>
    /// TestDayGroupData
    /// </summary>
    public class TestDayGroupData
    {
        /// <summary>
        /// Key
        /// </summary>
        public int Key { get; init; }

        /// <summary>
        /// Count
        /// </summary>
        public int Count { get; init; }
    }

    /// <summary>
    /// TestKKSGroupData
    /// </summary>
    public class TestKKSGroupData
    {
        /// <summary>
        /// KKS
        /// </summary>
        public string KKS { get; init; } = string.Empty;

        /// <summary>
        /// NAM
        /// </summary>
        public string NAM { get; init; } = string.Empty;

        /// <summary>
        /// Count
        /// </summary>
        public int Count { get; init; }

        /// <summary>
        /// ApprovedCount
        /// </summary>
        public int ApprovedCount { get; set; }

        /// <summary>
        /// Percent
        /// </summary>
        public double Percent { get; set; }
    }

}
